﻿using LdtDataEngin.ModuleDetail.AnalysisModule.ParagraphAnalysis;
using LdtDataEngin.ModuleDetail.CommonModule.XmlCodeCommon;
using LdtJudge.DataEngine.DataBaseStores;
using LdtJudge.DataEngine.Model.Collection;
using LdtJudge.DataEngine.Model.DataModel.DataParseStorage;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;

namespace LdtDataEngin.Moudle
{
    /// <summary>
    /// 段落提取模块
    /// </summary>
    public class MoudleParagraphExtract : MoudleBase
    {
        private IMoudle _moudle;

        private static List<An_duanluo> _duanluoList;
        /// <summary>
        /// 段落提取模块
        /// </summary>
        /// <param name="moudle">下一个模块</param>
        public MoudleParagraphExtract(IMoudle moudle)
        {
            _moudle = moudle;
        }
        /// <summary>
        /// 执行模块
        /// </summary>
        /// <param name="dataContext"></param>
        public override void MoudleHandle(DataContext dataContext)
        {
            //执行当前模块的相关业务逻辑；
            string moudleName = typeof(MoudleParagraphExtract).FullName;
            moudleName += "_" + this.GetHashCode();
            if(!dataContext.DataLayerAns.IsParagraphAnalysis.HasValue || dataContext.DataLayerAns.IsParagraphAnalysis==false)
            {
                // 设置模块进度
                dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName, 0, dataContext.InteractiveShow.inputParameterModel);
                // 创建计时器
                Stopwatch stopwatch = new Stopwatch();
                stopwatch.Start();
                //执行当前模块的相关业务逻辑
                try
                {
                    dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->开始执行段落提取模块*****************************");
                    XmlCodeStartModel xmlCodeStartModel = new XmlCodeStartModel() { dataContext = dataContext };
                    var configModel = dataContext.DataLayerSys.GetConfigModel(moudleName);
                    if (xmlCodeStartModel.Extract(configModel))
                    {
                        ParagraphAssemble paragraphAssemble = new ParagraphAssemble(dataContext, moudleName);
                        paragraphAssemble.Extrat();
                    }

                }
                catch (Exception ex)
                {
                    dataContext.InteractiveShow.LogShow("文本分段提取模块错误：" + ex.ToString(), LogMessageType.Error);
                }
                dataContext.InteractiveShow.LogShow("***************************" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + "--->执行段落提取模块完成*****************************");
                // 设置模块进度
                stopwatch.Stop();
                _duanluoList = dataContext.DataLayerAns.Duanluos;
                dataContext.InteractiveShow.ModuleProcessShow?.Invoke(moudleName + "_" + stopwatch.Elapsed.TotalMilliseconds.ToString("0"), 100, dataContext.InteractiveShow.inputParameterModel);
            }
            //执行完毕调用下一个模块；
            _moudle.MoudleHandle(dataContext);
        }

        /// <summary>
        /// 模块处理结果
        /// </summary>
        /// <returns></returns>
        public static HandleResultModel MoudleHandleResult()
        {
            HandleResultModel resultModel = new HandleResultModel();
            if (_duanluoList.Any())
            {
                resultModel.ResultType = "4";
                resultModel.ResultData = _duanluoList;
            }
            return resultModel;
        }
    }
}
